package org.richie144.ext4.ajax.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.richie144.ext4.ajax.domain.Emp;
import org.richie144.ext4.ajax.service.EmpService;
import org.richie144.ext4.ajax.service.impl.EmpServiceImpl;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

/**
 * 
 * 
 * 项目名称：ext4-panel-ajax
 * 类名称：EmpServlet
 * 类描述：emp 控制类
 * 创建人：任欢
 * 创建时间：2012-8-23 下午1:55:21
 * 修改人：任欢
 * 修改时间：2012-8-23 下午1:55:21
 * 修改备注：
 * @version 
 *
 */
public class EmpServlet extends HttpServlet {
	
	private static final long serialVersionUID = 255588888L;
	
	private static EmpService empService = new EmpServiceImpl();

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("text/javascript");
		List<Emp> empList = empService.getEmpList();
		long totalRecord = empService.count();
		JsonArray array = new JsonArray();
		JsonObject empJsonObject = null;
		for(Emp emp:empList) {
			empJsonObject = new JsonObject();
			empJsonObject.addProperty("empno", emp.getEmpno());
			empJsonObject.addProperty("ename", emp.getEname());
			empJsonObject.addProperty("job", emp.getJob());
			empJsonObject.addProperty("mgr", emp.getMgr());
			empJsonObject.addProperty("hiredate", emp.getHiredate().toString());
			empJsonObject.addProperty("sal", emp.getSal());
			empJsonObject.addProperty("comm", emp.getComm());
			empJsonObject.addProperty("deptno", emp.getDeptno());
			array.add(empJsonObject);
			empJsonObject = null;
		}
		
		JsonObject obj = new JsonObject();
		obj.addProperty("totals",totalRecord);
		obj.addProperty("success", true);
		obj.add("data",array);
		resp.getWriter().println(obj.toString());
	}	
}
